﻿using System;
using System.ComponentModel;
using System.ComponentModel.Composition;
using IssueVision.Common;
using IssueVision.EntityModel;
using IssueVision.WCFService.PasswordResetService;

namespace IssueVision.Model
{
    [Export(typeof(IPasswordResetModel))]
    [PartCreationPolicy(CreationPolicy.Shared)]
    public class PasswordResetModel : IPasswordResetModel
    {
        #region "Private Data"
        private PasswordResetServiceClient _proxy;
        #endregion "Private Data"

        #region "Constructor"
        public PasswordResetModel()
        {
            _proxy = PasswordResetServiceClient.Instance;
            // Set up event handling
            _proxy.PropertyChanged += _proxy_PropertyChanged;
            _proxy.GetPasswordQuestionByUserNameCompleted += _proxy_GetPasswordQuestionByUserNameCompleted;
            _proxy.ResetPasswordCompleted += _proxy_ResetPasswordCompleted;
        }
        #endregion "Constructor"

        #region "IPasswordResetModel Interface implementation"

        public void GetPasswordQuestionByUserNameAsync(string userName)
        {
            _proxy.GetPasswordQuestionByUserNameAsync(userName);
            _proxy.IncrementCallCount();
        }

        public void ResetPasswordAsync(PasswordResetUser user)
        {
            _proxy.ResetPasswordAsync(user);
            _proxy.IncrementCallCount();
        }

        /// <summary>
        /// True if at least one call is
        /// in progress; otherwise, false
        /// </summary>
        public Boolean IsBusy
        {
            get
            {
                return _isBusy;
            }

            private set
            {
                if (_isBusy != value)
                {
                    _isBusy = value;
                    OnPropertyChanged("IsBusy");
                }
            }
        }
        private Boolean _isBusy;

        public event EventHandler<ResultArgs<string>> GetPasswordQuestionCompleted;
        public event EventHandler<AsyncCompletedEventArgs> ResetPasswordCompleted;

        #endregion "IPasswordResetModel Interface implementation"

        #region "INotifyPropertyChanged Interface implementation"
        public event PropertyChangedEventHandler PropertyChanged;

        protected virtual void OnPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }
        #endregion "INotifyPropertyChanged Interface implementation"

        #region "Private Methods"

        /// <summary>
        /// Event handler for PropertyChanged
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void _proxy_PropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            if (e.PropertyName.Equals("ActiveCallCount"))
            {
                IsBusy = (_proxy.ActiveCallCount != 0);
            }
        }

        /// <summary>
        /// Event handler for GetPasswordQuestionByUserNameCompleted
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void _proxy_GetPasswordQuestionByUserNameCompleted(object sender, GetPasswordQuestionByUserNameCompletedEventArgs e)
        {
            _proxy.DecrementCallCount();

            if (GetPasswordQuestionCompleted != null)
            {
                GetPasswordQuestionCompleted(this, new ResultArgs<string>(e.Result, e.Error, e.Cancelled, e.UserState));
            }
        }

        /// <summary>
        /// Event handler for ResetPasswordCompleted
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void _proxy_ResetPasswordCompleted(object sender, AsyncCompletedEventArgs e)
        {
            _proxy.DecrementCallCount();

            if (ResetPasswordCompleted != null)
            {
                ResetPasswordCompleted(this, e);
            }
        }

        #endregion "Private Methods"
    }
}
